package com.ymatou.userservice.testcase;

import java.io.IOException;
import java.util.Calendar;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.VerificationCodeRequestDtoBean;
import com.ymatou.userservice.service.VerificationCodeRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.database.sqlwapper.YmtValidationCodeWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
*验证码的生成与校验接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_VerificationCodeRequestDto{
	private static VerificationCodeRequestDtoBean verificationcoderequestdtoBean;
	private static VerificationCodeRequestDtoCall verificationcoderequestdtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("验证码的生成与校验接口");
	}

	@Before
	public void caseUp() {
		verificationcoderequestdtoBean=new VerificationCodeRequestDtoBean();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(verificationcoderequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_VerificationCodeRequestDto_001() {
		Logger.start(true,"生成验证码成功");
		try {
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			String code = getValidateCode();			
			
			YmtValidationCodeWapper validationCodeWapper = new YmtValidationCodeWapper();
			String vlidateCode = validationCodeWapper.getValidateCode("13028899999");
			Logger.verifyEquals(vlidateCode, code, "验证生成的验证码");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_VerificationCodeRequestDto_002() {
		Logger.start(true,"验证码验证后设置为无效（Action=VerifyValidationCodeAndSetInvalid），再次验证无效-500");
		try {
			Logger.comment("生成验证码");
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			String code = getValidateCode();
			Logger.comment("进行第一次验证");    
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("GET", "PARAM");
			verificationcoderequestdtoBean.setVcode(code);
			verificationcoderequestdtoBean.setActionId("13028899999");
			verificationcoderequestdtoBean.setLength(6);
			verificationcoderequestdtoBean.setAction("VerifyValidationCodeAndSetInvalid");
			verificationcoderequestdtoCall.setData(verificationcoderequestdtoBean);
			verificationcoderequestdtoCall.callService();			
			Logger.verifyEquals("true", verificationcoderequestdtoCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(ECode.SUCCESS, verificationcoderequestdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("True", verificationcoderequestdtoCall.getString("Result"), "验证结果信息");
			
			Logger.comment("进行第二次验证");
			verificationcoderequestdtoCall.callService();
			Logger.verifyEquals("false", verificationcoderequestdtoCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("500", verificationcoderequestdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("已验证过", verificationcoderequestdtoCall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_VerificationCodeRequestDto_003() {
		Logger.start(true,"验证码验证后仍然保持有效（Action=VerifyValidationCodeAndKeepNormal），多次验证有效-200");
		try {
			Logger.comment("生成验证码");
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			String code = getValidateCode();
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("GET", "PARAM");
			verificationcoderequestdtoBean.setVcode(code);
			verificationcoderequestdtoBean.setActionId("13028899999");
			verificationcoderequestdtoBean.setLength(6);
			verificationcoderequestdtoBean.setAction("VerifyValidationCodeAndKeepNormal");
			verificationcoderequestdtoCall.setData(verificationcoderequestdtoBean);
			Logger.comment("进行10次验证");
			for(int i =0; i<10; i++){
				Logger.comment("第"+(i+1)+"次验证");
				verificationcoderequestdtoCall.callService();			
				Logger.verifyEquals("true", verificationcoderequestdtoCall.getString("Success"), "验证是否成功");
				Logger.verifyEquals(ECode.SUCCESS, verificationcoderequestdtoCall.getString("ResponseCode"), "验证Code");
				Logger.verifyEquals("True", verificationcoderequestdtoCall.getString("Result"), "验证结果信息");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_VerificationCodeRequestDto_004() {
		Logger.start(false, "验证码与电话号码不匹配，验证失败-100");
		try {
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			String code = getValidateCode();			
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("GET", "PARAM");
			verificationcoderequestdtoBean.setVcode(code+"0");
			verificationcoderequestdtoBean.setActionId("13028899999");
			verificationcoderequestdtoBean.setLength(6);
			verificationcoderequestdtoBean.setAction("VerifyValidationCodeAndSetInvalid");
			verificationcoderequestdtoCall.setData(verificationcoderequestdtoBean);
			verificationcoderequestdtoCall.callService();
			
			Logger.verifyEquals("false", verificationcoderequestdtoCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("100", verificationcoderequestdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("验证码错误", verificationcoderequestdtoCall.getString("LastErrorMessage"), "验证Msg信息");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	} 
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_VerificationCodeRequestDto_005() {
		Logger.start(true, "成功生成邮箱验证码");
		try {
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			verificationcoderequestdtoBean.setVcode("0");
			verificationcoderequestdtoBean.setActionId("12@ymatou.com");
			verificationcoderequestdtoBean.setLength(6);
			verificationcoderequestdtoBean.setTimeOut(5000l);
			verificationcoderequestdtoCall.setData(verificationcoderequestdtoBean);
			verificationcoderequestdtoCall.callService();
			
			Logger.verifyEquals("true", verificationcoderequestdtoCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(ECode.SUCCESS, verificationcoderequestdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("ok", verificationcoderequestdtoCall.getString("SuccessMessage"), "验证Msg信息");
			
			YmtValidationCodeWapper validationCodeWapper = new YmtValidationCodeWapper();
			String vlidateCode = validationCodeWapper.getValidateCode("12@ymatou.com");
			Logger.verifyEquals(vlidateCode, verificationcoderequestdtoCall.getString("Result"), "验证生成的验证码");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	} 
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_VerificationCodeRequestDto_006() {
		Logger.start(true, "成功生成邮箱验证码");
		try {
			String email = TestDataManager.getRandomNum(4)+"12@ymatou.com";
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			verificationcoderequestdtoBean.setVcode("0");
			verificationcoderequestdtoBean.setActionId(email);
			verificationcoderequestdtoBean.setLength(9);
			verificationcoderequestdtoBean.setTimeOut(5000l);
			verificationcoderequestdtoCall.setData(verificationcoderequestdtoBean);
			verificationcoderequestdtoCall.callService();
			
			Logger.verifyEquals("true", verificationcoderequestdtoCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(ECode.SUCCESS, verificationcoderequestdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("ok", verificationcoderequestdtoCall.getString("SuccessMessage"), "验证Msg信息");
			
			YmtValidationCodeWapper validationCodeWapper = new YmtValidationCodeWapper();
			String vlidateCode = validationCodeWapper.getValidateCode(email);
			Logger.verifyEquals(vlidateCode, verificationcoderequestdtoCall.getString("Result"), "验证生成的验证码");
			Logger.verifyEquals(9, vlidateCode.length(), "验证验证码的长度");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	} 
	
	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	//此case待确认
	public void Tc_VerificationCodeRequestDto_007() {
		Logger.start(true, "未输入验证码长度，默认生成6位");
		try {
			String email = TestDataManager.getRandomNum(4)+"12@ymatou.com";
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			verificationcoderequestdtoBean.setActionId(email);
			verificationcoderequestdtoBean.setTimeOut(5000l);
			verificationcoderequestdtoCall.setData(verificationcoderequestdtoBean);
			verificationcoderequestdtoCall.callService();
			
			Logger.verifyEquals("true", verificationcoderequestdtoCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(ECode.SUCCESS, verificationcoderequestdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("ok", verificationcoderequestdtoCall.getString("SuccessMessage"), "验证Msg信息");
			
			YmtValidationCodeWapper validationCodeWapper = new YmtValidationCodeWapper();
			String vlidateCode = validationCodeWapper.getValidateCode(email);
			Logger.verifyEquals(vlidateCode, verificationcoderequestdtoCall.getString("Result"), "验证生成的验证码");
			Logger.verifyEquals(6, vlidateCode.length(), "验证验证码的长度");
		} catch (Exception e) {
			Logger.fail(e);
		}
	} 
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_VerificationCodeRequestDto_008() {
		Logger.start(true, "未输入超时时间");
		try {
			String email = TestDataManager.getRandomNum(4)+"12@ymatou.com";
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			verificationcoderequestdtoBean.setVcode("0");
			verificationcoderequestdtoBean.setActionId(email);
			verificationcoderequestdtoBean.setLength(4);
			verificationcoderequestdtoCall.setData(verificationcoderequestdtoBean);
			verificationcoderequestdtoCall.callService();
			
			Logger.verifyEquals("true", verificationcoderequestdtoCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(ECode.SUCCESS, verificationcoderequestdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("ok", verificationcoderequestdtoCall.getString("SuccessMessage"), "验证Msg信息");
			
			YmtValidationCodeWapper validationCodeWapper = new YmtValidationCodeWapper();
			String vlidateCode = validationCodeWapper.getValidateCode(email);
			Logger.verifyEquals(vlidateCode, verificationcoderequestdtoCall.getString("Result"), "验证生成的验证码");
			Logger.verifyEquals(4, vlidateCode.length(), "验证验证码的长度");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_VerificationCodeRequestDto_009() {
		Logger.start(true,"验证验证码-验证码已失效-300");
		try {
			Logger.comment("生成验证码");
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			String code = getValidateCode();
			
			new YmtValidationCodeWapper().setiValidateStatus(code, -1);
			
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("GET", "PARAM");
			verificationcoderequestdtoBean.setVcode(code);
			verificationcoderequestdtoBean.setActionId("13028899999");
			verificationcoderequestdtoBean.setLength(6);
			verificationcoderequestdtoBean.setAction("VerifyValidationCodeAndKeepNormal");
			verificationcoderequestdtoCall.setData(verificationcoderequestdtoBean);
		
				verificationcoderequestdtoCall.callService();			
				Logger.verifyEquals("false", verificationcoderequestdtoCall.getString("Success"), "验证是否成功");
				Logger.verifyEquals("300", verificationcoderequestdtoCall.getString("ResponseCode"), "验证Code");
				Logger.verifyEquals("False", verificationcoderequestdtoCall.getString("Result"), "验证结果信息");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_VerificationCodeRequestDto_010() {
		Logger.start(true,"验证验证码-验证码验证错误-400");
		try {
			Logger.comment("生成验证码");
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			String code = getValidateCode();
			
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("GET", "PARAM");
			// verificationcoderequestdtoBean.setVcode(code);
			verificationcoderequestdtoBean.setActionId("13028899999");
			verificationcoderequestdtoBean.setLength(6);
			verificationcoderequestdtoBean.setAction("VerifyValidationCodeAndKeepNormal");
			verificationcoderequestdtoCall.setData(verificationcoderequestdtoBean);
		
				verificationcoderequestdtoCall.callService();			
				Logger.verifyEquals("false", verificationcoderequestdtoCall.getString("Success"), "验证是否成功");
				Logger.verifyEquals("400", verificationcoderequestdtoCall.getString("ResponseCode"), "验证Code");
				Logger.verifyEquals("验证验证码错误", verificationcoderequestdtoCall.getString("Result"), "验证结果信息");
				Logger.verifyEquals("验证验证码错误", verificationcoderequestdtoCall.getString("LastErrorMessage"), "验证结果信息");
				
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_VerificationCodeRequestDto_011() {
		Logger.start(true,"10分钟内生成验证码相同");
		try {
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			String code1 = getValidateCode();			
			
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			String code2 = getValidateCode();
			
			Logger.verifyEquals(code2, code1, "验证生成的验证码相同");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_VerificationCodeRequestDto_012() {
		Logger.start(true,"10分钟后生成验证码不同");
		try {
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			String code1 = getValidateCode();	
			
			Thread.sleep(700000);
			
			verificationcoderequestdtoCall=new VerificationCodeRequestDtoCall("POST", "JSON");
			String code2 = getValidateCode();
			
			Logger.verifyNotEquals(code2, code1, "验证生成的验证码不相同");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	/**
	 * 生成验证码
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	private String getValidateCode() throws ClientProtocolException, IOException{
		verificationcoderequestdtoBean.setVcType("Number");
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR, 1);
		verificationcoderequestdtoBean.setTimeOut(calendar.getTimeInMillis());
		verificationcoderequestdtoBean.setLength(6);
		verificationcoderequestdtoBean.setIncludeLevel("BindMobile");
		verificationcoderequestdtoBean.setActionId("13028899999");
		verificationcoderequestdtoCall.setData(verificationcoderequestdtoBean);
		verificationcoderequestdtoCall.callService();		
		
		return verificationcoderequestdtoCall.getString("Result");
	} 
}